package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.LeaseAgreementService;
import com.atguigu.lease.web.app.vo.agreement.AgreementDetailVo;
import com.atguigu.lease.web.app.vo.agreement.AgreementItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;

    //公寓详情
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    //房间详情
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    //公寓图片
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    //房间图片

    //租期详情
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    //支付方式
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;

    @Override
    public List<AgreementItemVo> selectAgreementItemVoListByUserId(String phone) {
        return leaseAgreementMapper.selectAgreementItemVoListByUserId(phone);

    }

    @Override
    public AgreementDetailVo getAgreementDetailVoById(Long id) {
        //查询租约详情
        LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(id);
        if (leaseAgreement == null) {
            return null;
        }
        //公寓详情
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectApartmentById(leaseAgreement.getApartmentId());
        //房间详情
        RoomInfo roomInfo = roomInfoMapper.selectRoomById(leaseAgreement.getRoomId());
        //公寓图片
        List<GraphVo> graphVoApartmentList = graphInfoMapper.selectGraphInfoByItemTypeAndId(ItemType.APARTMENT, leaseAgreement.getApartmentId());
        //房间图片
        List<GraphVo> graphVoRoomList = graphInfoMapper.selectGraphInfoByItemTypeAndId(ItemType.ROOM, leaseAgreement.getRoomId());
        //租期详情
        LeaseTerm leaseTermList = leaseTermMapper.selectLeaseTermById(leaseAgreement.getLeaseTermId());
        //支付方式
        PaymentType paymentTypeList = paymentTypeMapper.selectListById(leaseAgreement.getPaymentTypeId());

        //复制装配
        AgreementDetailVo agreementDetailVo = new AgreementDetailVo();
        BeanUtils.copyProperties(leaseAgreement,agreementDetailVo);
        agreementDetailVo.setApartmentName(apartmentInfo.getName());
        agreementDetailVo.setRoomNumber(roomInfo.getRoomNumber());
        agreementDetailVo.setApartmentGraphVoList(graphVoApartmentList);
        agreementDetailVo.setRoomGraphVoList(graphVoRoomList);
        agreementDetailVo.setLeaseTermMonthCount(leaseTermList.getMonthCount());
        agreementDetailVo.setPaymentTypeName(paymentTypeList.getName());
        agreementDetailVo.setLeaseTermUnit(leaseTermList.getUnit());
        return agreementDetailVo;
    }

}




